package wiz.client.activities;

import wiz.client.ClientFactory;
import wiz.client.activities.entrypoint.EntryPointActivity;
import wiz.client.activities.entrypoint.EntryPointPlace;
import wiz.client.activities.finishedgame.FinishedGameActivity;
import wiz.client.activities.finishedgame.FinishedGamePlace;
import wiz.client.activities.playgame.GameInProgressPlace;
import wiz.client.activities.playgame.PlayGameActivity;
import wiz.client.activities.playhand.PlayHandActivity;
import wiz.client.activities.playhand.PlayHandPlace;
import wiz.client.activities.selectplayers.SelectPlayersActivity;
import wiz.client.activities.selectplayers.SelectPlayersPlace;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;

public class WizActivityMapper implements ActivityMapper {

	private ClientFactory fClientFactory;
	private EntryPointActivity fEntryPointActivity;
	private SelectPlayersActivity fSelectPlayersActivity;
	private PlayGameActivity fPlayGameActivity;
	private PlayHandActivity fPlayHandActivity;
	private FinishedGameActivity fFinishedGameActivity;

	public WizActivityMapper(ClientFactory clientFactory) {
		fClientFactory = clientFactory;
	}

	@Override
	public Activity getActivity(Place place) {
		if(place instanceof EntryPointPlace){
			EntryPointActivity entryPointActivity = getEntryPointActivity();
			entryPointActivity.setGameStateToken(((EntryPointPlace)place).getGameStateToken());
			return entryPointActivity;
		} else if(place instanceof SelectPlayersPlace){
			SelectPlayersActivity selectPlayersActivity = getSelectPlayersActivity();
			selectPlayersActivity.setGameStateToken(((SelectPlayersPlace)place).getGameStateToken());
			return selectPlayersActivity;
		} else if(place instanceof GameInProgressPlace) {
			PlayGameActivity playGameActivity = getPlayGameActivity();
			playGameActivity.setGameStateToken(((GameInProgressPlace)place).getGameStateToken());
			return playGameActivity;
		} else if (place instanceof PlayHandPlace) {
			PlayHandActivity playHandActivity = getPlayHandActivity();
			playHandActivity.setGameStateToken(((PlayHandPlace)place).getGameStateToken());
			return playHandActivity;
		} else if(place instanceof FinishedGamePlace){
			FinishedGameActivity finishedGameActivity = getFinishedGameActivity();
			finishedGameActivity.setGameStateToken(((FinishedGamePlace)place).getGameStateToken());
			return finishedGameActivity;
		} else {
			//TODO: Log error
			return getEntryPointActivity();
		}
	}

	private FinishedGameActivity getFinishedGameActivity() {
		if(fFinishedGameActivity == null){
			fFinishedGameActivity = new FinishedGameActivity(fClientFactory);
		}
	
		return fFinishedGameActivity;
	}

	private PlayHandActivity getPlayHandActivity() {
		if(fPlayHandActivity == null){
			fPlayHandActivity = new PlayHandActivity(fClientFactory);
		}
		return fPlayHandActivity;
	}

	private PlayGameActivity getPlayGameActivity() {
		if(fPlayGameActivity == null){
			fPlayGameActivity = new PlayGameActivity(fClientFactory);
		}
		return fPlayGameActivity;
	}

	private SelectPlayersActivity getSelectPlayersActivity() {
		if(fSelectPlayersActivity == null){
			fSelectPlayersActivity = new SelectPlayersActivity(fClientFactory);
		}
		return fSelectPlayersActivity;
	}

	private EntryPointActivity getEntryPointActivity() {
		if(fEntryPointActivity == null){
			fEntryPointActivity = new EntryPointActivity(fClientFactory);
		}
		return fEntryPointActivity;
	}

}
